package com.exam.web.action.privilege;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.exam.bean.privilege.Department;
import com.exam.bean.PageView;
import com.exam.bean.QueryResult;
import com.exam.service.privilege.DepartmentService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller("departmentAction")
@Scope("prototype")
public class DepartmentAction extends ActionSupport {
	
	private static final long serialVersionUID = 6832608356647095265L;

	@Resource
	private DepartmentService departmentService;
	
	
	private int page;//分页
	private String query;//查询
	private PageView<Department> pageView;//记录集
	private Department department;//组织结构
	private Integer id;//id
	private Integer parentid;//父级id
	private Integer[] departmentids;
	
	/**
	 * 组织结构显示
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="department",privilegeValue="view")
	public String list()throws Exception{
		
		PageView<Department> pageView=new PageView<Department>(12,this.getPage());
		int firstIndex=(this.getPage()-1)*pageView.getMaxResult();
		
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		
		StringBuffer jpql=new StringBuffer("");
		List<Object> params=new ArrayList<Object>();
		if("true".equals(this.getQuery())){
			if(this.getDepartment().getName()!=null && !"".equals(this.getDepartment().getName().trim())){
				jpql.append("o.name like ?"+ (params.size()+1));
				params.add("%"+ this.getDepartment().getName().trim()+ "%");
			}
			else{
				QueryResult<Department> qr=departmentService.getScrollData(Department.class
						,firstIndex,pageView.getMaxResult(),orderby);
				pageView.setQueryResult(qr);
				this.setPageView(pageView);
				return SUCCESS;
			}
		}
		else{
			if(this.getParentid()!=null&&this.getParentid()>0){
				jpql.append("o.parent.id=?"+(params.size()+1));
				params.add(this.getParentid());
			}
			else{
				jpql.append("o.parent is null");
			}
		}
		
		QueryResult<Department> qr=departmentService.getScrollData(Department.class
				,firstIndex,pageView.getMaxResult(),jpql.toString(),params.toArray(),orderby);
		
		pageView.setQueryResult(qr);
		this.setPageView(pageView);
		
		return SUCCESS;
	}

	/**
	 * 添加组织结构
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="department",privilegeValue="insert")
	public String add()throws Exception{
		
		Department department=this.getDepartment();
		if(this.getParentid()!=null&&this.getParentid()>0){
			department.setParent(new Department(this.getParentid()));
		}
		departmentService.save(department);
		ActionContext.getContext().put("message", "添加组织结构成功");
		ActionContext.getContext().put("urladdress", "department_list.html");
		return "message";
	}
	
	/**
	 * 添加组织结构界面
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="department",privilegeValue="insert")
	public String addUI()throws Exception{
		
		return "add";
	}
	
	/**
	 * 修改组织结构
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="department",privilegeValue="update")
	public String edit()throws Exception{
		
		Department department=departmentService.find(Department.class, this.getDepartment().getId());
		department.setName(this.getDepartment().getName());
		departmentService.update(department);
		ActionContext.getContext().put("message", "修改组织结构成功");
		ActionContext.getContext().put("urladdress", "department_list.html");
		return "message";
	}
	
	/**
	 * 修改组织结构界面
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="department",privilegeValue="update")
	public String editUI()throws Exception{
		
		Department department=departmentService.find(Department.class, this.getId());
		this.setDepartment(department);
		return "edit";
	}
	
	/**
	 * 启用组织结构
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="department",privilegeValue="visible")
	public String visible()throws Exception{
		
		departmentService.setVisible(this.getDepartmentids(), true);
		ActionContext.getContext().put("message", "启用成功");
		ActionContext.getContext().put("urladdress", "department_list.html");
		return "message";
	}
	
	/**
	 * 禁用组织结构
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="department",privilegeValue="disvisible")
	public String disvisible()throws Exception{
		
		departmentService.setVisible(this.getDepartmentids(), false);
		ActionContext.getContext().put("message", "禁用成功");
		ActionContext.getContext().put("urladdress", "department_list.html");
		return "message";
	}
	
	
	public int getPage() {
		return page<1?1:page;
	}
	public void setPage(int page) {
		this.page = page;
	}

	public String getQuery() {
		return query;
	}
	public void setQuery(String query) {
		this.query = query;
	}

	public PageView<Department> getPageView() {
		return pageView;
	}
	public void setPageView(PageView<Department> pageView) {
		this.pageView = pageView;
	}

	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getParentid() {
		return parentid;
	}
	public void setParentid(Integer parentid) {
		this.parentid = parentid;
	}

	public Integer[] getDepartmentids() {
		return departmentids;
	}
	public void setDepartmentids(Integer[] departmentids) {
		this.departmentids = departmentids;
	}
}
